package com.daisuxia.back.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.daisuxia.back.service.IBackUserService;
import com.daisuxia.back.utils.DwzResult;
import com.daisuxia.back.utils.SpringUtils;
import com.daisuxia.constant.Constant;
import com.daisuxia.risk.utils.ConstantRisk;
import com.daisuxia.web.controller.BaseController;
import com.daisuxia.web.pojo.BackLog;
import com.daisuxia.web.pojo.BackUser;
import com.daisuxia.web.pojo.User;
import com.daisuxia.web.service.IUserService;
import com.daisuxia.web.util.PageConfig;

import redis.clients.jedis.JedisCluster;

@Controller
@RequestMapping("customService/")
public class CustomServiceController extends BaseController {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private IUserService userService;
	@Autowired
	private IBackUserService backUserService;
	@Autowired
	JedisCluster jedisCluster;

	@RequestMapping("findJxl/{interval}")
	public String findJxl(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Integer interval) {
		HashMap<String, Object> params = getParametersO(request);
		try {
			params.put("interval", interval);
			PageConfig<User> pageConfig = userService.findJxlList(params);
			model.addAttribute("pm", pageConfig);
			model.addAttribute("params", params);
		} catch (Exception e) {
			logger.error("findJxl error params=" + params, e);
		}
		return "custom/jxlList";
	}

	@RequestMapping("resetJxl")
	public void resetJxl(HttpServletRequest request, HttpServletResponse response, Model model) {
		HashMap<String, Object> params = this.getParametersO(request);
		boolean flag = false;
		String errMsg = "未知异常！";
		Integer id = Integer.parseInt(params.get("id").toString());
		String key = ConstantRisk.JXL_REPORT + id;
		try {
			if (jedisCluster.get(key) != null) {
				errMsg = "当前数据正在被处理,请稍后重试！";
			} else {
				if (userService.findJxlStatus(id) > 0) {
					errMsg = "当前用户已完成运营商认证！";
				} else {
					jedisCluster.setex(key, ConstantRisk.FLAG_SECOND, id + "");
					userService.updateJxl(id);
					backUserService.insertLog(new BackLog(loginAdminUser(request).getId(), "resetJxl", id + ""));
					flag = true;
					errMsg = "重置成功！";
					jedisCluster.del(key);
				}
			}
		} catch (Exception e) {
			jedisCluster.del(key);
			logger.error("resetJxl error", e);
		}
		SpringUtils.renderDwzResult(response, flag, errMsg, DwzResult.CALLBACK_CLOSECURRENTDIALOG, params.get("parentId").toString());
	}

}
